package com.ruoyi.aimodel.controller;

import com.ruoyi.aimodel.domain.AipptOutline;
import com.ruoyi.aimodel.service.EduAiService;
import com.ruoyi.aimodel.service.IEwcDeepSeekAccessService;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.validation.Valid;
import java.io.IOException;
import java.util.UUID;


@Slf4j
@Validated
@RestController
@Tag(name = "ai模型", description = "ai模型")
@RequestMapping("/api")
public class DoubaoController {


    @Autowired
    private IEwcDeepSeekAccessService ewcDeepSeekAccessService;
    @Autowired
    private EduAiService eduAiService;

    @GetMapping("/deepSeek/deepSeekAccessRec")
    public SseEmitter getChatMessageStream(@RequestParam String  msg) {

        SseEmitter resultSse=ewcDeepSeekAccessService.getChatMessageStream(msg);
        return resultSse;
    }
    //流式豆包
    @PostMapping("/getDoubaoStream")
    public  SseEmitter getDoubaoStream(@RequestBody AipptOutline aipptOutline) {
    SseEmitter emitter = new SseEmitter(180000L);
        if(aipptOutline.getUserid().isEmpty()){
            log.info("userid为空，临时会话");
            aipptOutline.setUserid(UUID.randomUUID().toString());
        }
    try {
        eduAiService.getDoubaoStream(aipptOutline.getUserid(),aipptOutline.getContent())
                .subscribe(
                        content -> {
                            try {
                                emitter.send(content, MediaType.TEXT_PLAIN);
                            } catch (IOException e) {
                                log.info("发送内容错误: " + e.getMessage());
                                emitter.completeWithError(e);
                            }
                        },
                        error -> {
                            try {
                                log.info("处理出现异常: " + error.getMessage());
                                emitter.send(SseEmitter.event().data("Error: " + error.getMessage()));
                            } catch (IOException e) {
                                log.info("发送了异常信息: " + e.getMessage());
                                emitter.completeWithError(e);
                            } finally {
                                emitter.complete();
                            }
                        },
                        () -> {
                            emitter.complete();
                        }
                );
    } catch (Exception e) {
        try {
            log.info("Unexpected error: " + e.getMessage());
            emitter.send(SseEmitter.event().data("Error: " + e.getMessage()));
        } catch (IOException ex) {
            log.info("Error sending unexpected error message: " + ex.getMessage());
            emitter.completeWithError(ex);
        } finally {
            emitter.complete();
        }
    }

    return emitter;
    }

//豆包非流式请求
    @PostMapping("/getDoubaoChat")
    public String getDoubaoChat(@RequestBody AipptOutline aipptOutline) {
        if(aipptOutline.getUserid().isEmpty()){
            log.info("userid为空，临时会话");
            aipptOutline.setUserid(UUID.randomUUID().toString());
        }
        String returnMsg=eduAiService.getDoubaoChat(aipptOutline.getUserid(),aipptOutline.getContent());
        log.info(returnMsg);
        return returnMsg;
    }
    @PostMapping("/chatPost")
    public String chatPost(@Valid @RequestBody AipptOutline aipptOutline) {
        String aa=eduAiService.getDoubaoChat(aipptOutline.getUserid(),aipptOutline.getMsg());
        log.info(aa);
        return aa;
    }

}

